package com.tongji.controller;


import com.bean.Mess;
import com.bean.WmsTCrDetails;
import com.bean.WmsTCustomer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tongji.service.TongjiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TongjiController {
    @Autowired
    TongjiService tongjiService;

    //库存盘点
    @RequestMapping("KuCunPanDian.do")
    public String KuCunPanDian(HttpServletRequest request){
        int pageNum2 = 0;
        int pageRows = 5;
        PageHelper.startPage(pageNum2,pageRows);
        String custName="lalala";
        List<WmsTCustomer> wmsTCustomers = tongjiService.getAllCustomers(custName);
        PageInfo<WmsTCustomer> pageInfo1 = new PageInfo<>(wmsTCustomers);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        if(request.getSession().getAttribute("custName")!=null){
            request.getSession().removeAttribute("custName");
        }
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list1",list);
        request.getSession().setAttribute("pageInfo1",pageInfo1);
        // 仓库：显示仓库中有东西的那些仓库信息：如果仓库中100个位置trayState全部都是0，仓库不能显示
        List<String> stringList=tongjiService.getAllCangKu();
        request.getSession().setAttribute("stringList",stringList);
        return "tongji/KuCunPanDian";
    }
    //库存盘点
    @RequestMapping("kucunpandian.do")
    public String getcangku(String storeName,String pageNum,String custName,int pageRows,HttpServletRequest request){
        System.out.println(custName);
        System.out.println(storeName);
        int pageNum1=1;
        request.getSession().setAttribute("custName",custName);
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        System.out.println("pageRows:"+pageRows);

        PageHelper.startPage(pageNum1,pageRows);
        List<WmsTCustomer> wmsTCustomerList=tongjiService.getHaveSomething(custName,storeName);

        //    0 开始盘点：修改需求，往WMS_T_CR_DETAILS插入一条记录，将具体的details的信息插入该表
        //    chkrptID对应wms_t_bill_details中的details_id字段；
        //    detailsID自动生成，其他和wms_t_bill_details保持一致
        //    detailsEditTime：系统时间
        PageInfo<WmsTCustomer> pageInfo2 = new PageInfo<>(wmsTCustomerList);
        WmsTCrDetails wmsTCrDetails=new WmsTCrDetails();

        for (int i=0;i<wmsTCustomerList.size();i++){
            String chkrpID=wmsTCustomerList.get(i).getDetailsID();
            String detailsGoodsName=wmsTCustomerList.get(i).getDetailsGoodsName();
            Double detailsGoodsSpec=wmsTCustomerList.get(i).getDetailsGoodsSpec();
            String s= UUID.randomUUID().toString().replace("-","");
            String detailsID=s.substring(0,6);
            Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYY-MM-dd");
            String detailsEditTime=simpleDateFormat.format(date);

            System.out.println(chkrpID);
            System.out.println(detailsGoodsName);
            System.out.println(detailsID);
            System.out.println(detailsEditTime);
            System.out.println(detailsGoodsSpec);

            wmsTCrDetails.setChkrptId(chkrpID);
            wmsTCrDetails.setDetailsGoodsName(detailsGoodsName);
            wmsTCrDetails.setDetailsSpec(detailsGoodsSpec);
            wmsTCrDetails.setDetailsId(detailsID);
            wmsTCrDetails.setDetailsEditTime(detailsEditTime);
            List<Map<Object,Object>> list=tongjiService.getWmsTCrDetails(wmsTCrDetails);
            if (list==null||list.size()==0){
                tongjiService.insertWmsTCrDetails(wmsTCrDetails);
                System.out.println("我到插入了");
            }else{
                break;
            }
        }
        if(pageInfo2!=null){
            System.out.println(pageInfo2);
            request.getSession().setAttribute("pageInfo1",pageInfo2);
        }
        System.out.println("wmsTCustomerList:"+wmsTCustomerList);
        return "tongji/KuCunPanDian";
    }


    //货主查询
    @RequestMapping("HuoZhuChaXun.do")
    public String HuoZhuChaXun(HttpServletRequest request){
        int pageNum2 = 0;
        int pageRows = 5;
        PageHelper.startPage(pageNum2,pageRows);
        String custName="lalala";
        List<WmsTCustomer> wmsTCustomers = tongjiService.getAllCustomers(custName);
        PageInfo<WmsTCustomer> pageInfo1 = new PageInfo<>(wmsTCustomers);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        if(request.getSession().getAttribute("huozhu")!=null){
            request.getSession().removeAttribute("huozhu");
        }
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list1",list);

        request.getSession().setAttribute("pageInfo1",pageInfo1);
        List<String> stringList=tongjiService.getHuoZhuAllCangKu();
        request.getSession().setAttribute("stringList",stringList);
        return "tongji/HuoZhuChaXun";}
    //根据姓名模糊查询数据(货主查询)
    @RequestMapping("selectCustomer.do")
    @ResponseBody
    public List<WmsTCustomer> selectCustomer(String custName){
        System.out.println("前端数据:"+custName);
        List<WmsTCustomer> wmsTCustomer=tongjiService.getAllCust(custName);
        System.out.println("模糊查询:"+wmsTCustomer);
        return wmsTCustomer;
    }
    //货主查询的开始盘点
    @RequestMapping("huozhuchaxun1.do")
    public String getOne(String pageNum,int pageRows,HttpServletRequest request){
        int pageNum1=1;
        String huozhu=request.getParameter("huozhu");
        System.out.println("huozhu:"+huozhu);
        String[] strings=huozhu.split(":");
        String telephone=strings[0];
        String custName=strings[1];
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);

        request.getSession().setAttribute("huozhu",huozhu);

        List<WmsTCustomer> wmsTCustomer=tongjiService.getOne(telephone,custName);
        PageInfo<WmsTCustomer> pageInfo1 = new PageInfo<>(wmsTCustomer);
        if(pageInfo1!=null){
            System.out.println(pageInfo1);
            request.getSession().setAttribute("pageInfo1",pageInfo1);
        }
        return "tongji/HuoZhuChaXun";
    }


    //盘点查询历史
    @RequestMapping("PanDianHistory.do")
    public String PanDianHistory(HttpServletRequest request){
        int pageNum2 = 0;
        int pageRows = 5;
        PageHelper.startPage(pageNum2,pageRows);
        String detailsID="0";
        List<WmsTCrDetails> wmsTCrDetailsList = tongjiService.getOneWmsTCrDetails(detailsID);
        PageInfo<WmsTCrDetails> pageInfo1 = new PageInfo<>(wmsTCrDetailsList);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        if(request.getSession().getAttribute("custID")!=null){
            request.getSession().removeAttribute("custID");
        }
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list1",list);

        request.getSession().setAttribute("pageInfo1",pageInfo1);
        return "tongji/PanDianHistory";}
    //根据客户ID模糊查询数据(盘点记录查询)
    @RequestMapping("selectCustomerBycustID.do")
    @ResponseBody
    public List<WmsTCustomer> selectCustomerBycustID(String custID){
        System.out.println("前端custID数据:"+custID);
        List<WmsTCustomer> wmsTCustomer=tongjiService.getAllCustByCustID(custID);
        System.out.println("custID模糊查询:"+wmsTCustomer);
        return wmsTCustomer;
    }
    //开始查询
    @RequestMapping("pandianhistory.do")
    public String getAllWmsTCrDetails(String custID,String date,String date2,String pageNum,int pageRows,HttpServletRequest request){
        int pageNum1=1;
        request.getSession().setAttribute("custID",custID);
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);
        List<Mess> messList=tongjiService.getAllWmsTCrDetails(custID,date,date2);
        System.out.println("messList:"+messList);
        PageInfo<Mess> pageInfo1 = new PageInfo<>(messList);
        if(pageInfo1!=null){
            System.out.println("pageInfo1:"+pageInfo1);
            request.getSession().setAttribute("pageInfo1",pageInfo1);
        }

        return "tongji/PanDianHistory";
    }


    //货主id盘点
//    @RequestMapping("custIDpandian.do")
//    public String getcustBycustID(String custID,String pageNum,int pageRows,HttpServletRequest request){
//
//        int pageNum1=1;
//        request.getSession().removeAttribute("custID");
//        request.getSession().setAttribute("custID",custID);
//        if(pageNum!=null){
//            pageNum1=Integer.parseInt(pageNum);
//        }
//        request.getSession().setAttribute("pageRows",pageRows);
//        System.out.println("pageRows:"+pageRows);
//        PageHelper.startPage(pageNum1,pageRows);
//        List<WmsTCustomer> wmsTCustomerList=tongjiService.HaveSomething(custID);
//        PageInfo<WmsTCustomer> pageInfo2 = new PageInfo<>(wmsTCustomerList);
//        if(pageInfo2!=null){
//            System.out.println(pageInfo2);
//            request.getSession().setAttribute("pageInfo2",pageInfo2);
//        }
//        System.out.println("wmsTCustomerList:"+wmsTCustomerList);
//        return "tongji/PanDianHistory";
//    }

}
